import { constantRoutes, asyncRoutes } from '@/router'
export default {
  namespaced: true,
  state() {
    return {
      routes: [...constantRoutes]
    }
  }, getters: {},
  mutations: {
    setRoutes(state, data) {
      state.routes = [...constantRoutes, ...data]
    }
  }, actions: {
    filterRoutes(context, menuMarks) {
      console.log(menuMarks)
      const userAsyncRoutes = []
      console.log(menuMarks)
      // console.log(asyncRoutes)
      asyncRoutes.forEach(item => {
        if (menuMarks.find(obj => obj.mark === item.name)) {
          if (item.children && item.children.length) {
            const temp = item.children.filter(val => {
              return menuMarks.find(o => o.mark === val.name)
            })
            item.children = temp
          }
        }
        userAsyncRoutes.push(item)
      })
      context.commit('setRoutes', userAsyncRoutes)
      console.log(userAsyncRoutes)
      return userAsyncRoutes
    }
    // 思路二
    // generateRoutes(context, menuMarks) {
    //   const userAsyncRoutes = []
    //   menuMarks.forEach(item => {
    //     const obj = {}
    //     obj.meta = {
    //       title: item.title,
    //       icon: item.icon
    //     }
    //     obj.hidden = item.is_show !== 1
    //     obj.path = item.path
    //     if (item.compont === 'layout') {
    //       obj.component = require('@/layout/index.vue').default
    //     } else {
    //       obj.component = require('@/view/' + item.component + '.vue').default
    //     }
    //     obj.id = item.id
    //     obj.pid = item.pid
    //     userAsyncRoutes.push(obj)
    //   })
    //   userAsyncRoutes = tranListToTreeDate(userAsyncRoutes, 0)
    //   context.commit('setRouter', userAsyncRoutes)
    //   return userAsyncRoutes
    // }
  }
}
